package com.ilop.sthome.ui.activity.device;

import android.text.TextUtils;
import android.view.View;

import com.example.common.base.BaseBindActivity;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.AutomationBean;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.greenDao.RoomBean;
import com.ilop.sthome.data.bean.WarnBean;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.DeviceStatusUtil;
import com.ilop.sthome.utils.greenDao.utils.AutomationDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.RoomDaoUtil;
import com.example.common.utils.DateUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityDeviceMessageBinding;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @author skygge
 * @date 2020/5/27.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：消息详情页
 */
public class DeviceMessageActivity extends BaseBindActivity<ActivityDeviceMessageBinding> {

    private WarnBean mWarnBean;
    private String mRoomName;
    private String mGatewayName;
    private String mDeviceName;
    private String mStatus;
    private List<String> mInput;
    private List<String> mOutput;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_message;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mWarnBean = (WarnBean) getIntent().getSerializableExtra(CommonId.KEY_MESSAGE);
    }

    @Override
    protected void initView() {
        super.initView();
        mInput = new ArrayList<>();
        mOutput = new ArrayList<>();
    }

    @Override
    protected void initData() {
        super.initData();
        mDBind.tvMessageTime.setText(DateUtil.getDateToString(mWarnBean.getTime()*1000));
        if (mWarnBean.getDevice_id() == 0){
            isGatewayAlarm();
        }else {
            isSubDeviceAlarm();
        }
    }

    private void isGatewayAlarm() {
        DeviceInfoBean gateway = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mWarnBean.getDeviceName());
        if (gateway!=null) {
            if (TextUtils.isEmpty(gateway.getNickName())) {
                mGatewayName = getString(R.string.ali_gateway);
            } else {
                mGatewayName = gateway.getNickName();
            }
            if (!TextUtils.isEmpty(gateway.getRoom_id())) {
                RoomBean roomBean = RoomDaoUtil.getInstance().getRoomByRoomId(gateway.getRoom_id());
                if (roomBean.getNickname() != null){
                    mRoomName = roomBean.getNickname();
                }else {
                    mRoomName = ConfigureUtil.roomName(mContext, roomBean.getRid());
                }
            }else if (gateway.getOwned() != 1){
                mRoomName = getString(R.string.ali_gateway_share);
            }else {
                mRoomName = getString(R.string.default_room);
            }
        }
        if ("AC".equals(mWarnBean.getType())){
            AutomationBean mAuto = AutomationDaoUtil.getInstance().findAutomationByMid(mWarnBean.getMid(), mWarnBean.getDeviceName());
            if (mAuto!=null) {
                List<DeviceInfoBean> mInputList = mAuto.getInputList();
                List<DeviceInfoBean> mOutputList = mAuto.getOutputList();
                if (mInputList!=null && mInputList.size() > 0) {
                    for (DeviceInfoBean device : mInputList) {
                        mInput.add(mContext.getString(SmartDevice.getType(device.getDevice_type()).getTypeStrId()));
                    }
                }
                if (mOutputList!=null && mOutputList.size() > 0) {
                    for (DeviceInfoBean device : mOutputList) {
                        mOutput.add(mContext.getString(SmartDevice.getType(device.getDevice_type()).getTypeStrId()));
                    }
                }
            }
        }else {
            mStatus = DeviceStatusUtil.getGatewayAlert(mContext, mWarnBean.getDevice_status());
        }
        mDBind.llWhatHappen.setVisibility(View.GONE);
        mDBind.llDeviceName.setVisibility(View.GONE);
        mDBind.tvMessageRoom.setText(mRoomName);
        mDBind.tvMessageGateway.setText(mGatewayName);
        mDBind.tvMessageTrigger.setText(StringUtils.join(mInput.toArray(),"\t-\t"));
        mDBind.tvMessageAction.setText(StringUtils.join(mOutput.toArray(),"\t-\t"));
    }

    private void isSubDeviceAlarm() {
        DeviceInfoBean deviceInfoBean = DeviceDaoUtil.getInstance().findByDeviceId(mWarnBean.getDeviceName(),mWarnBean.getDevice_id());
        if (deviceInfoBean!=null){
            if(!TextUtils.isEmpty(deviceInfoBean.getSubdeviceName())){
                mDeviceName = deviceInfoBean.getSubdeviceName();
            }else {
                mDeviceName = getString(SmartDevice.getType(mWarnBean.getDevice_type()).getTypeStrId())+mWarnBean.getDevice_id();
            }
            if (!TextUtils.isEmpty(deviceInfoBean.getRoom_id())) {
                RoomBean roomBean = RoomDaoUtil.getInstance().getRoomByRoomId(deviceInfoBean.getRoom_id());
                if (roomBean.getNickname() != null){
                    mRoomName = roomBean.getNickname();
                }else {
                    mRoomName = ConfigureUtil.roomName(mContext, roomBean.getRid());
                }
            }else {
                DeviceInfoBean gateway = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mWarnBean.getDeviceName());
                if (gateway.getOwned() != 1){
                    mRoomName = getString(R.string.ali_gateway_share);
                }else {
                    mRoomName = getString(R.string.default_room);
                }
            }
            DeviceInfoBean gateway = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mWarnBean.getDeviceName());
            if (TextUtils.isEmpty(gateway.getNickName())) {
                mGatewayName = getString(R.string.ali_gateway);
            } else {
                mGatewayName = gateway.getNickName();
            }
            mStatus = DeviceStatusUtil.getAlert(mContext,mWarnBean.getDevice_type(),mWarnBean.getDevice_status());
        }

        mDBind.llIsAuto.setVisibility(View.GONE);
        mDBind.tvMessageName.setText(mDeviceName);
        mDBind.tvMessageRoom.setText(mRoomName);
        mDBind.tvMessageGateway.setText(mGatewayName);
        mDBind.tvMessageDetail.setText(mStatus);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivMessageBack.setOnClickListener(v -> finish());
    }
}
